package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 检查组controller
 */
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @Autowired
    private JedisPool jedisPool;

    //新增检查项
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        //System.out.println(checkGroup);
        //System.out.println(checkitemIds);
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            redisUtil(RedisConstant.ADD_CHECKGROUP);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //分页查询检查项
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = checkGroupService.findPage(queryPageBean);
            redisUtil(RedisConstant.SELECT_CHECKGROUP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    //回显编辑框数据
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")//权限校验
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            redisUtil(RedisConstant.SELECT_CHECKGROUP);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //查询关联检查项
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            redisUtil(RedisConstant.UPDATE_CHECKGROUP);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    //删除检查组
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_DELETE')")//权限校验
    @RequestMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(Integer id){
        try {
            checkGroupService.deleteCheckGroup(id);
            redisUtil(RedisConstant.DELETE_CHECKGROUP);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //查询所有的检查组
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")//权限校验
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroupList=checkGroupService.findAll();
            redisUtil(RedisConstant.SELECT_CHECKGROUP);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    public  void redisUtil(String name) throws Exception {
        if (jedisPool.getResource().zrank(DateUtils.parseDate2String(DateUtils.getToday()), name) == null) {
            jedisPool.getResource().zadd(DateUtils.parseDate2String(DateUtils.getToday()), 1, name);
        } else {
            Double zscore = jedisPool.getResource().zscore(DateUtils.parseDate2String(DateUtils.getToday()), name);
            jedisPool.getResource().zadd(DateUtils.parseDate2String(DateUtils.getToday()), zscore + 1, name);
        }
    }
}
